package com.sac.approval.service.impl;

import com.sac.approval.dao.LineDao;
import com.sac.approval.domain.LineDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.sac.approval.dao.LineDetailDao;
import com.sac.approval.domain.LineDetailDO;
import com.sac.approval.service.LineDetailService;
import org.springframework.transaction.annotation.Transactional;


@Service
public class LineDetailServiceImpl implements LineDetailService {
	@Autowired
	private LineDetailDao lineDetailDao;

	@Autowired
	private LineDao lineDao;
	
	@Override
	public LineDetailDO get(Integer lineDetailId){
		return lineDetailDao.get(lineDetailId);
	}
	
	@Override
	public List<LineDetailDO> list(Map<String, Object> map){
		return lineDetailDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return lineDetailDao.count(map);
	}
	
	@Override
	public int save(LineDetailDO lineDetail){
		return lineDetailDao.save(lineDetail);
	}

	@Override
	public int update(LineDetailDO lineDetail){
		return lineDetailDao.save(lineDetail);
	}

	@Override
	@Transactional
	public int batchSave(LineDetailDO[] lineDetails) {
		Arrays.asList(lineDetails).forEach(lineDetail->{
			remove(lineDetail.getLineDetailId());
			save(lineDetail);
		});
		return lineDetails.length;
	}

	@Override
	@Transactional
	public int saveNewLine(LineDO line, List<LineDetailDO> LineDetail){
		lineDao.save(line);
		LineDetail.forEach(lineDetail->{
				lineDetail.setLineId(line.getLineId());
				save(lineDetail);
			}
		);
		return LineDetail.size();
	}

	@Override
	@Transactional
	public int updateByLineId(LineDO line, List<LineDetailDO> LineDetail){
		lineDao.update(line);
		lineDetailDao.removeByLineId(line.getLineId());
		LineDetail.forEach(lineDetail->
				save(lineDetail)
		);
		return LineDetail.size();
	}
	
	@Override
	public int remove( Integer lineDetailId){
		return lineDetailDao.remove(lineDetailId);
	}
	
	@Override
	public int batchRemove(Integer[] lineDetailIds){
		return lineDetailDao.batchRemove(lineDetailIds);
	}
	
}
